package ru.urfc.portal.ui.client.googlets.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Widget;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.Constants;
import ru.urfc.portal.ui.client.controllers.ErrorReporter;
import ru.urfc.portal.ui.client.controllers.ItemsController;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;

public class BalanceSource extends BaseGooglet {

	interface Binder extends UiBinder<Widget, BalanceSource> {}
	private static Binder binder = GWT.create(Binder.class);

	@UiField Element template;
	@UiField Element clientName;
	@UiField Element balance;
	
	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
		Element target = DOM.getElementById(Constants.BALANCE_TARGET_ID);
		if (target != null) {
			initBalance(target);
		}
	}

	private void initBalance(Element target) {
		target.appendChild(template);

		GridDescriptor descriptor = new GridDescriptor(BALANCE_OPERATION,
				CLIENTS, getClientId(), BALANCE_OPERATIONS, LAST_BALANCE_OPERATION);
		descriptor.addTextField("clientName", clientName);
		descriptor.addMoneyField("balance", balance).getDblRenderer().setMoneyFormat();
		final ItemsController controller = createController(descriptor);
		controller.setErrorReporter(new ErrorReporter() {
			@Override
			public void reportError(String message) {
				clientName.getStyle().setColor("#666666");
				clientName.setInnerText(message);
				balance.getStyle().setColor("#666666");
				balance.setInnerText(message);
			}
		});
	}

}